﻿using System;
using SmartSiteManagementSystem.IRepository.ProjectManage;
using SmartSiteManagementSystem.Models.ProjectManage;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.ProjectManage
{
    public class DayProjectTypeInfoRepository : IDayProjectTypeInfoRepository
    {
        #region dataTable
        /*
drop table if exists `day_project_type`;
create table `day_project_type`( 
	`day_project_type_id` int(11) not null AUTO_INCREMENT primary key, 
	`day_project_type_name` nvarchar(250) NOT NULL,
    `is_delete` bit NOT NULL default 0,
	`create_time` datetime not null default now(),
	`update_time` datetime not null default now()
) default charset=utf8;
         */
        #endregion
       // private IDbConnection _readConn;
        //private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from day_project_type";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("day_project_type_id"))
                    {
                        pars.Add("day_project_type_id", param["day_project_type_id"]);

                        whereCondition.Append(" day_project_type_id =@day_project_type_id");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if (backInt > 0)
                {
                    back = true;
                }
            }
            return back;
        }

        public DayProjectTypeInfo Get(Hashtable param)
        {
            DayProjectTypeInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT day_project_type_id as DayProjectTypeId,day_project_type_name as DayProjectTypeName,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM day_project_type";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("day_project_type_id"))
                    {
                        pars.Add("day_project_type_id", param["day_project_type_id"]);
                        whereCondition.Append(" and day_project_type_id =@day_project_type_id");
                    }
                    if (param.ContainsKey("day_project_type_name"))
                    {
                        pars.Add("day_project_type_name", param["day_project_type_name"]);
                        whereCondition.Append(" and day_project_type_name =@day_project_type_name");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<DayProjectTypeInfo>(execSql, pars);
            }
            return back;
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(day_project_type_id) from day_project_type";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("day_project_id"))
                    {
                        pars.Add("day_project_type_id", param["day_project_type_id"]);
                        whereCondition.Append(" and day_project_type_id =@day_project_type_id");
                    }
                    if (param.ContainsKey("day_project_type_name"))
                    {
                        pars.Add("day_project_type_name", param["day_project_type_name"]);
                        whereCondition.Append(" and day_project_type_name =@day_project_type_name");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<DayProjectTypeInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {

            IList<DayProjectTypeInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT day_project_type_id as DayProjectTypeId,day_project_type_name as DayProjectTypeName,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM day_project_type";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("day_project_id"))
                    {
                        pars.Add("day_project_type_id", param["day_project_type_id"]);
                        whereCondition.Append(" and day_project_type_id =@day_project_type_id");
                    }
                    if (param.ContainsKey("day_project_type_name"))
                    {
                        pars.Add("day_project_type_name", param["day_project_type_name"]);
                        whereCondition.Append(" and day_project_type_name =@day_project_type_name");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by day_project_type_id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<DayProjectTypeInfo>(execSql, pars).AsList<DayProjectTypeInfo>();
            }
            return back;
        }

        public int Insert(DayProjectTypeInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"INSERT INTO day_project_type
(day_project_type_name,
    is_delete,
	create_time,
	update_time
)
VALUES
(
    @day_project_type_name,
    @is_delete,
	@create_time,
	@update_time);select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    day_project_type_name = obj.DayProjectTypeName,
                    is_delete = obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
            }
            return back;
        }

        public bool Update(DayProjectTypeInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update day_project_type set
    day_project_type_name=@day_project_type_name ,
    is_delete=@is_delete,
	create_time=@create_time,
	update_time=@update_time
WHERE day_project_type_id =@day_project_type_id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                int backint = _writeConn.Execute(sql, new
                {
                    day_project_type_id = obj.DayProjectTypeId,
                    day_project_type_name = obj.DayProjectTypeName,
                    is_delete = obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
                if (backint > 0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

